package com.example.administrator.caigou51.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.ab.util.AbViewUtil;
import com.alibaba.fastjson.JSON;
import com.example.administrator.caigou51.EventBusClass.GotoFragment;
import com.example.administrator.caigou51.EventBusClass.RefreshOrderListActivity;
import com.example.administrator.caigou51.EventBusClass.UpdateOrdeListTopTitle;
import com.example.administrator.caigou51.R;
import com.example.administrator.caigou51.bean.OrderInfoBean;
import com.example.administrator.caigou51.global.App;
import com.example.administrator.caigou51.global.Constant;
import com.example.administrator.caigou51.recyclerCard.basic.MaterialListView;
import com.example.administrator.caigou51.recyclerCard.card.OrderListItemCard;
import com.example.administrator.caigou51.recyclerCard.card.OrderTypeCard;
import com.example.administrator.caigou51.request.BaseResponse;
import com.example.administrator.caigou51.request.ICallBackForRequestServer;
import com.example.administrator.caigou51.request.RequestServerManager;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.event.EventBus;

/**
 * Created by Administrator on 2016/5/20/020.
 */
public class OrderListActivity extends GBaseActivity{
    MaterialListView materialListviewType;
    MaterialListView materialListview;
    LinearLayout linearLayoutEmpty;
    TextView textViewSuiBian;
    public static final int ALL=0;//全部
    public static final int DFK=1;//待付款
    public static final int DFH=2;//待发货
    public static final int DSH=3;//待收货
    public static final int JYWG=4;//交易完成
    public static final int YQX_canshu=-1;//已取消
    public static final int YQX_1=8;//已取消
    public static final int YQX_2=9;//已取消
    public static final int HDFK=23;//货到付款,待审核
    public static String TagOrderType=ALL+"";
    public String currentType=ALL+"";
    public boolean isFromTradeSuccess=false;
    public static String TagFromTradeSuccess="TagFromTradeSuccess";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentViewFor720_1280(R.layout.activity_order_list);
        findViewByIds();
        initDatas();
        fillArrayInListViewOrderType();
        fillArrayInListView(App.getOrderList(),currentType+"");
        TaskGetOrderList(false);
    }


    protected void findViewByIds() {
        materialListview= (MaterialListView) findViewById(R.id.materialListview);
        materialListviewType= (MaterialListView) findViewById(R.id.materialListviewType);
        linearLayoutEmpty= (LinearLayout) findViewById(R.id.linearLayoutEmpty);
        textViewSuiBian= (TextView) findViewById(R.id.textViewSuiBian);textViewSuiBian.setOnClickListener(this);
    }


    protected void initDatas() {


        EventBus.getDefault().register(this);
        setTopTitle("全部订单");
        isFromTradeSuccess=getIntent().getBooleanExtra(TagFromTradeSuccess,false);
        setRightImg(View.VISIBLE,R.drawable.icon_more_net_white);
        if (!TextUtils.isEmpty(getIntent().getStringExtra(TagOrderType))) {
            currentType = getIntent().getStringExtra(TagOrderType);
        }



        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
        materialListviewType.setLayoutManager(linearLayoutManager);

    }

    protected void fillArrayInListViewOrderType() {

        int smoothPosition=0;
        for (int i=0;i<6;i++) {

            OrderInfoBean orderInfoBean = new OrderInfoBean();
            if (i==5)
                orderInfoBean.setStatus(YQX_1+"");
            else
                orderInfoBean.setStatus(i + "");
            OrderTypeCard orderTypeCard = new OrderTypeCard(this);
            orderTypeCard.setMaterialListView(materialListviewType);
            if (currentType.equals(i+""))
            {
                orderTypeCard.setChecked(true);
                smoothPosition=i;
            }
            if (i==0)
            {
                orderTypeCard.setAllOrder(true);
            }

            orderTypeCard.setOrderInfoBean(orderInfoBean);
            materialListviewType.add(orderTypeCard);
            materialListviewType.smoothScrollToPosition(smoothPosition);
        }
    }

    protected void fillArrayInListView(List<OrderInfoBean> orderInfoBeenList,String currentStatus) {




            materialListview.clear();
            for (int i = 0; i < orderInfoBeenList.size(); i++) {

                OrderInfoBean orderInfoBean = orderInfoBeenList.get(i);

                //已取消有2个标志
                if (currentStatus.equals(YQX_1+""))
                {
                    if (currentStatus.equals(orderInfoBean.getStatus()) || (YQX_2+"").equals(orderInfoBean.getStatus())) {
                        OrderListItemCard orderListItemCard = new OrderListItemCard(this);
                        orderListItemCard.setOrderInfoBean(orderInfoBean);
                        materialListview.add(orderListItemCard);
                    }
                }
                //待发货有2个标志
                else if (currentStatus.equals(DFH+""))
                {
                    if (currentStatus.equals(orderInfoBean.getStatus()) || (HDFK+"").equals(orderInfoBean.getStatus())) {
                        OrderListItemCard orderListItemCard = new OrderListItemCard(this);
                        orderListItemCard.setOrderInfoBean(orderInfoBean);
                        materialListview.add(orderListItemCard);
                    }
                }
                //筛选出特定状态订单
                else if (currentStatus.equals(orderInfoBean.getStatus())) {
                    OrderListItemCard orderListItemCard = new OrderListItemCard(this);
                    orderListItemCard.setOrderInfoBean(orderInfoBean);
                    materialListview.add(orderListItemCard);
                } else if (currentStatus.equals(ALL + "")) {
                    OrderListItemCard orderListItemCard = new OrderListItemCard(this);
                    orderListItemCard.setOrderInfoBean(orderInfoBean);
                    materialListview.add(orderListItemCard);
                }


            }
        if (materialListview.getAdapter().getItemCount()==0)
        {
            materialListview.setVisibility(View.GONE);
            linearLayoutEmpty.setVisibility(View.VISIBLE);
        }else {
            materialListview.setVisibility(View.VISIBLE);
            linearLayoutEmpty.setVisibility(View.GONE);
        }

    }
    public void onEventMainThread(RefreshOrderListActivity refreshOrderListActivity) {
        TaskGetOrderList(false);
    }
    public void onEventMainThread(UpdateOrdeListTopTitle updateOrdeListTopTitle) {


        if (updateOrdeListTopTitle.isAllOrder())
        {
            setTopTitle("全部订单");
            currentType=ALL+"";
        }else {

            if (updateOrdeListTopTitle.getOrderInfoBean().getStatus().equals(DFK+""))
            {
                setTopTitle("待付款");
            }else if (updateOrdeListTopTitle.getOrderInfoBean().getStatus().equals(DFH+""))
            {
                setTopTitle("待发货");
            }else if (updateOrdeListTopTitle.getOrderInfoBean().getStatus().equals(DSH+""))
            {
                setTopTitle("待收货");
            }else if (updateOrdeListTopTitle.getOrderInfoBean().getStatus().equals(JYWG+""))
            {
                setTopTitle("交易完成");
            }else if (updateOrdeListTopTitle.getOrderInfoBean().getStatus().equals(YQX_1+"") || updateOrdeListTopTitle.getOrderInfoBean().getStatus().equals(YQX_2+""))
            {
                setTopTitle("已取消");
            }else if (updateOrdeListTopTitle.getOrderInfoBean().getStatus().equals(HDFK+""))
            {
                setTopTitle("货到付款");
            }

            currentType=updateOrdeListTopTitle.getOrderInfoBean().getStatus();

        }

        fillArrayInListView(App.getOrderList(),currentType);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    private void TaskGetOrderList(final boolean isloadmore) {
        RequestServerManager.getInstanceDIY().handleMethodDIY((GBaseActivity) getActivity(), materialListview, isloadmore, null,
                Constant.getRootUrl() + Constant.Action.Action_OrderList, Constant.Action.Action_OrderList, RequestServerManager.getInstanceDIY().
                        getRequestParams(Constant.Action.Action_OrderList, App.getUserBean().getUserid(),App.getUserBean().getSession_id(),"","1","1000"), BaseResponse.class, new ICallBackForRequestServer<BaseResponse>() {
                    @Override
                    public void onSuccess(BaseResponse result) {
                        // JSONObject jsonObject = (JSONObject) result.getData();
                       List<OrderInfoBean> orderInfoBeanList = JSON.parseArray(result.getData().toString(), OrderInfoBean.class);
                        App.saveOrderList(orderInfoBeanList);
                        App.getOrderList().clear();
                        App.getOrderList().addAll(orderInfoBeanList);
                        fillArrayInListView(App.getOrderList(),currentType);
                   //     Log.e("userBean",JSON.toJSONString(result.getData()));
                    }

                    @Override
                    public void onFail(int type, String errorMsg, String json) {

                    }
                });
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId())
        {
            case R.id.textViewSuiBian:
                Intent intent =new Intent(OrderListActivity.this,MainActivity.class);
                startActivity(intent);
                EventBus.getDefault().post(new GotoFragment(MainActivity.TAB_ShouYe));
                if (!this.getLocalClassName().contains("MainActivity"))
                    finish();
                break;
        }
    }

    @Override
    public void doClickRightImg() {
        super.doClickRightImg();
        App.showPopupWindow(getRightImageView(),OrderListActivity.this,-124,-30);
    }

    @Override
    public void doClickLeftImg() {
        if (isFromTradeSuccess) {
            Intent intent=new Intent(this, MainActivity.class);
            startActivity(intent);
        }else
        {
            super.doClickLeftImg();
        }
    }

    @Override
    public void onBackPressed() {
        if (isFromTradeSuccess) {
            Intent intent=new Intent(this, MainActivity.class);
            startActivity(intent);
        }else
        {
            super.onBackPressed();
        }

    }
}
